package com.roc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;
import com.roc.bean.TransLimitBean;
import com.roc.bean.orgMainBean;
import com.roc.dao.DaoException;
import com.roc.dao.IBaseDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.dao.ISysLogDao;
import com.roc.dao.IorgDao;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.pojo.SysLog;
import com.roc.pojo.TblArea;
import com.roc.pojo.TblInsInf;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.util.constant.ConfigMerinfoUtil;
import com.roc.util.constant.Constant;
import com.roc.util.constant.SysLogType;
@Service
@Transactional
public class OrgServiceImpl implements IorgService{
	private Logger logger=Logger.getLogger(OrgServiceImpl.class);
	@Autowired
	private IorgDao orgDao;
	@Autowired
	private ISysLogDao sysLogDao;
	@Autowired
	ISQLBaseDao sqlBaseDao;
	@Autowired
	private IBaseDao baseDao;
	@Override
	public Map<String, Object> addOrg(TblInsInf bean,Operators op)
			throws ServiceException {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
		String msg="";
		//增加机构信息
		String sql=" insert into tbl_ins_inf    "
				+"  (ins_id_cd,ins_company_cd,ins_type,ins_name,ins_prov_cd,  "
				+"   ins_city_cd,ins_region_cd,ins_sta,ins_stlm_tp,ins_alo_stlm_cycle,  "
				+"   ins_alo_stlm_md,ins_stlm_c_nm,ins_stlm_c_acct,ins_stlm_c_bk_no,  "
				+"   ins_stlm_c_bk_nm,ins_stlm_d_nm,ins_stlm_d_acct,ins_stlm_d_bk_no,  "
				+"   ins_stlm_d_bk_nm,rec_opr_id,rec_upd_opr,rec_crt_ts)  "
				+" values  "
				+"  ('48502' || LPAD(SEQ_Tbl_Ins_Id.Nextval, 3, '0'),'48502' || LPAD(SEQ_Tbl_Ins_Id.Currval, 3, '0'),'1',?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,cast(sysdate as timestamp))";

		List<Object> parmas=new ArrayList<Object>();
		parmas.add(bean.getInsName());
		parmas.add(bean.getInsProvCd());
		parmas.add(bean.getInsCityCd());
		parmas.add(bean.getInsRegionCd());
		parmas.add("1");
		parmas.add(bean.getInsStlmTp().toString().split(",")[0]);
		parmas.add(bean.getInsAloStlmCycle().toString().split(",")[0]);
		parmas.add(bean.getInsAloStlmMd().toString().split(",")[0]);
		parmas.add(bean.getInsStlmCNm());
		parmas.add(bean.getInsStlmCAcct());
		parmas.add(bean.getInsStlmCBkNo());
		parmas.add(bean.getInsStlmCBkNm());
		parmas.add(bean.getInsStlmDNm());
		parmas.add(bean.getInsStlmDAcct());
		parmas.add(bean.getInsStlmDBkNo());
		parmas.add(bean.getInsStlmDBkNm());
		parmas.add("I");
		parmas.add(op.getAccountNo());
		int count=sqlBaseDao.deleteBySql(sql, parmas);
		if(count>0)
		{
			msg="添加机构信息成功";
			logger.info(msg);
			baseDao.addSyslog(SysLogType.ORG_ADD.getCode(), SysLogType.ORG_ADD.getName(), op.getAccountNo(),op.getAccountNo()+"添加机构"+bean.getInsName()+"信息成功");
		}
		} catch (DaoException e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("添加机构信息失败");
		}
		return resultMap;
	}
	@Override
	public int updateOrg(TblInsInf bean, Operators op)
			throws ServiceException {
		int count=0;
		try {
		String sql= " update  tbl_ins_inf    "
					+"  set ins_name=?, ins_prov_cd=?,ins_city_cd=?, "//ins_prov_cd=?,ins_city_cd=?,ins_region_cd=?
					+"   ins_sta=?,ins_stlm_tp=?,ins_alo_stlm_cycle=?,  "
					+"   ins_alo_stlm_md=?,ins_stlm_c_nm=?,ins_stlm_c_acct=?,ins_stlm_c_bk_no=?,  "
					+"   ins_stlm_c_bk_nm=?,ins_stlm_d_nm=?,ins_stlm_d_acct=?,ins_stlm_d_bk_no=?,  "
					+"   ins_stlm_d_bk_nm=?,rec_opr_id='U',rec_upd_opr=?,rec_upd_ts=cast(sysdate as timestamp)  "
					+" where  ins_id_cd=? ";
		List<Object> parmas=new ArrayList<Object>();
		parmas.add(bean.getInsName());
		parmas.add(bean.getInsProvCd());
		parmas.add(bean.getInsCityCd());
		//parmas.add(bean.getInsRegionCd());
		parmas.add(bean.getInsSta().toString().split(",")[0]);
		parmas.add(bean.getInsStlmTp().toString().split(",")[0]);
		parmas.add(bean.getInsAloStlmCycle().toString().split(",")[0]);
		parmas.add(bean.getInsAloStlmMd().toString().split(",")[0]);
		parmas.add(bean.getInsStlmCNm());
		parmas.add(bean.getInsStlmCAcct());
		parmas.add(bean.getInsStlmCBkNo());
		parmas.add(bean.getInsStlmCBkNm());
		parmas.add(bean.getInsStlmDNm());
		parmas.add(bean.getInsStlmDAcct());
		parmas.add(bean.getInsStlmDBkNo());
		parmas.add(bean.getInsStlmDBkNm());
		parmas.add(op.getAccountNo());
		parmas.add(bean.getInsIdCd());
		count=sqlBaseDao.deleteBySql(sql, parmas);
		if(count>0){
			logger.info("修改机构信息成功!");
			baseDao.addSyslog(SysLogType.ORG_UPDATE.getCode(), SysLogType.ORG_UPDATE.getName(), op.getAccountNo(),op.getAccountNo()+"修改机构【"+bean.getInsIdCd()+"】信息成功");
			}
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return count;
	}
	
	@Override
	public List<TblInsInf> queryOrgList() throws ServiceException {
		List<TblInsInf> orgList= new ArrayList<TblInsInf>();
		try {
			orgList=orgDao.queryOrgList();
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载机构失败~！");
		}
		
		return orgList;
	}
	@Override
	public List<TblInsInf> queryAllOrgList() throws ServiceException {
		List<TblInsInf> orgList= new ArrayList<TblInsInf>();
		try {
			orgList=orgDao.queryAllOrgList();
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载机构失败~！");
		}
		
		return orgList;
	}
	@Override
	public OrgBean queryOrgByCode(String org_code) throws ServiceException {
		OrgBean ob=null;
		try {
			ob=orgDao.queryOrgByCode(org_code);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载机构失败~！");
		}
		
		return ob;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List lisOrgByCode(String org_code) throws ServiceException {
		List orgList= new ArrayList();
		
		try {
			orgList=orgDao.queryAll(" from OrgBean t where t.org_code='"+org_code+"'");
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载机构失败~！");
		}
		
		return orgList;
	}
	
	
	@Override
	public List lisOrgByMerchantId(long merchantId) throws ServiceException {
		
		List<OrgBean> orgBeanList=new ArrayList<OrgBean>();
		try {
			orgBeanList=orgDao.lisOrgByMerchantId(merchantId);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载机构信息出错");
		}
		return orgBeanList;
	}
	@Override
	public List<OrgBean> listOrgs(Operators operators) throws ServiceException {
		List<OrgBean> orgBeanList=new ArrayList<OrgBean>();
		try {
			orgBeanList=orgDao.listOrgs(operators);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载机构信息出错");
		}
		return orgBeanList;
	}

	@Override
	public List<TblInsInf> listOrg(TblInsInf inf) throws ServiceException {
		List<TblInsInf> orgBeanList=new ArrayList<TblInsInf>();
		try {
			orgBeanList=orgDao.queryOrg(inf);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载机构信息出错");
		}
		return orgBeanList;
	}
	@Override
	public List<TblArea> listAreaByOrgCode(String orgCode) throws ServiceException {
		List<TblArea> areaList=new ArrayList<TblArea>();
		try {
			areaList=orgDao.listAreaByOrgCode(orgCode);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载区域信息出错");
		}
		return areaList;
	}
	@Override
	public TblInsInf queryOrgByOrgCode(String org_code) throws ServiceException {
		TblInsInf ob=null;
		try {
			ob=orgDao.queryOrgByOrgCode(org_code);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载机构失败~！");
		}
		
		return ob;
	}
	@Override
	public TblInsInf queryInsByCode(String ins_code) throws ServiceException {
		TblInsInf ob=null;
		try {
			ob=orgDao.queryInsByCode(ins_code);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载机构失败~！");
		}
		
		return ob;
	}
	@Override
	public List<orgMainBean> listOrgMainByOrgCode(String orgCode) throws ServiceException {
		List<orgMainBean> orgMainList = new ArrayList<orgMainBean>();
		try {
			orgMainList = orgDao.listOrgMainByOrgCode(orgCode);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载小机构信息出错");
		}
		return orgMainList;
	}
	@Override
	public orgMainBean queryOrgMainByOrgMain(String orgMain) throws ServiceException {
		orgMainBean ob=null;
		try {
			ob=orgDao.queryOrgMainByOrgMain(orgMain);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("加载机构失败~！");
		}
		
		return ob;
	}

}
